package com.zzw.spring.aop.service;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;

public class ControllablePerformanceMonitor extends DelegatingIntroductionInterceptor implements Mointorable{

	private ThreadLocal<Boolean> monitorStatusMap = new ThreadLocal<Boolean>();
	
	@Override
	public void setMonitorAdvice(boolean active) {
		monitorStatusMap.set(active);
	}
	
	@Override
	public Object invoke(MethodInvocation mi) throws Throwable{
		Object obj = null;
		if(monitorStatusMap.get() != null && monitorStatusMap.get()){
			System.out.println(mi.getClass().getName() + "." + mi.getMethod().getName());
			obj = super.invoke(mi);
			System.out.println("end");
		}
		else {
			obj = super.invoke(mi);
		}
		return obj;
	}
}
